package com.zy.baselib.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.zy.baselib.R;


/**
 * 通用对话框类
 */
public class TipDialog extends AlertDialog {

    private String title;
    private String message;
    private String strPositive;
    private String strNegative;
    private OnDialogButtonClickListener listener;
    private boolean cancelable = true;
    private boolean canDismiss = true;

    private int layoutId;


    /**
     * @param context
     * @param title
     * @param listener
     */
    public TipDialog(Context context, String title, String message, OnDialogButtonClickListener listener) {
        this(context, title, message, "取消", "确定", listener);
    }

    /**
     * @param context
     * @param title
     * @param strPositive "确定"类按钮文本
     * @param strNegative "取消"类按钮文本
     * @param listener
     */
    public TipDialog(Context context, String title, String message,
                     String strPositive, String strNegative, OnDialogButtonClickListener listener) {
        super(context, R.style.UpdateDialog);
        this.title = title;
        this.message = message;
        this.strPositive = strPositive;
        this.strNegative = strNegative;
        this.listener = listener;
    }

    public TipDialog(Context context, @LayoutRes int layout, String title, String message,
                     String strPositive, String strNegative, OnDialogButtonClickListener listener) {
        this(context, title, message, strPositive, strNegative, listener);
        layoutId = layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(layoutId != 0) {
            setContentView(layoutId);
        } else {
            setContentView(R.layout.layout_dialog_hint);
        }
        //alertDialog是否可以点击外围消失
        setCanceledOnTouchOutside(cancelable);
        setCancelable(cancelable);

        TextView tvTitle = findViewById(R.id.text_title);
        if(tvTitle != null) {
            tvTitle.setText(title);
        }
        TextView tvMessage = findViewById(R.id.text_message);
        if(tvMessage != null) {
            tvMessage.setText(message);
        }
        TextView mBtnLeft = findViewById(R.id.btn_left);
        if(mBtnLeft != null) {
            mBtnLeft.setText(strPositive);
            mBtnLeft.setOnClickListener(this::onClick);
        }
        TextView mBtnRight = findViewById(R.id.btn_right);
        if(mBtnRight != null) {
            mBtnRight.setText(strNegative);
            mBtnRight.setOnClickListener(this::onClick);
        }
        View closeView = findViewById(R.id.btn_close);
        if(closeView != null) {
             closeView.setOnClickListener(this::onClick);
        }

        //如果title为空,则隐藏alertDialog标题
        final WindowManager.LayoutParams params = this.getWindow().getAttributes();
        this.getWindow().getDecorView().setPadding(0, 0, 0, 0);
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;
        this.getWindow().setAttributes(params);
    }

    public @Nullable
    TextView getMessageView() {
        return findViewById(R.id.text_message);
    }

    public void onClick(final View v) {
        if (v.getId() == R.id.btn_left) {
            if(listener != null) {
                listener.onDialogButtonClick(this, true);
            }
        } else if (v.getId() == R.id.btn_right) {
            if(listener != null) {
                listener.onDialogButtonClick(this, false);
            }
        }
        if(isCanDismiss()) {
            dismiss();
        }
    }

    public boolean isCanDismiss() {
        return canDismiss;
    }

    public void setCanDismiss(boolean canDismiss) {
        this.canDismiss = canDismiss;
    }

    public OnDialogButtonClickListener getListener () {
        return listener;
    }

    /**
     * 自定义Dialog监听器
     */
    public interface OnDialogButtonClickListener {
        /**
         * 点击按钮事件的回调方法
         *
         * @param isPositive
         */
        void onDialogButtonClick(TipDialog dialog, boolean isPositive);

    }
}
